package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.entity.Sku;
import com.leyou.item.pojo.dto.SkuDTO;
import com.leyou.item.pojo.dto.SpuDTO;
import com.leyou.item.pojo.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 查询商品信息
 */
//spu/page?key=h&saleable=true&page=1&rows=5
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /*返回商品查询大致结果*/
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "key",required = false)String key,    //查询条件不一定要有
            @RequestParam(value = "saleable",required = false)Boolean saleable, //是否上架
            @RequestParam(value = "page",defaultValue = "1")Integer page,  //当前页
            @RequestParam(value = "rows",defaultValue = "5")Integer rows   //每页数据条数
    ){
        return ResponseEntity.ok().body(goodsService.querySpuByPage(key,saleable,page,rows));
    }

    /**
     * 新增商品
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){
        goodsService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品
     * @param saleable
     * @param spuId
     * @return
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam(value = "saleable",defaultValue = "false")Boolean saleable,
            @RequestParam(value = "id")Long spuId
    ){
        goodsService.updateSaleable(saleable,spuId);
        return ResponseEntity.ok().build();
    }

    /*商品详情回显*/
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long spuDetailId){
        return ResponseEntity.ok().body(goodsService.querySpuDetailById(spuDetailId));
    }

    /*商品特殊信息回显  查询sku*/
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long spuId){
        return ResponseEntity.ok().body(goodsService.querySkuListBySpuId(spuId));
    }

    /*修改商品*/
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spuId查询spu信息 spuDetail skus信息
     * @param spuId
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySpuById(spuId));
    }

    /**
     * 根据skuIds集合查询sku集合
     * @param skuIds
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids")List<Long> skuIds){
        return ResponseEntity.ok(this.goodsService.querySkuListByIds(skuIds));
    }

    /**
     * 减库存
     *
     * @param cartMap 商品skuId及数量的map
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartMap) {

        this.goodsService.minusStock(cartMap);
        return ResponseEntity.ok().build();
    }
}
